package com.kirk.cts.core.context;

import com.kirk.cts.core.runcycle.ICrawlerToolRunCycle;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.ApplicationContext;

import java.util.HashMap;
import java.util.Map;

/**
 * <p> CrawlerToolContextData.java </p>
 *
 * @version 1.0.0
 * @Author kirkliu
 * @Date 2020-08-29 22:21
 * @Description 上下文数据
 */
public final class CrawlerToolContextData {
    /**
     * 主线程的线程锁，用于防止主线程关闭
     */
    private Object crawlerToolTheadLock = new Object();

    /**
     * CrawlerTool运行周期
     */
    private ICrawlerToolRunCycle crawlerToolRunCycle;

    private static CrawlerToolContextData instance = new CrawlerToolContextData();

    private Thread swtThread;

    /**
     * spring加载上下文
     */
    private ApplicationContext applicationContext;

    /**
     * 插件集合
     */
    private static Map<String, Map<String, String>> plugins;

    /**
     * 上下文对象
     */
    private static Map<String, Object> contextData = new HashMap<String, Object>();


    public Map<String, Map<String, String>> getPlugins() {
        return plugins;
    }

    public void setPlugins(Map<String, Map<String, String>> plugins) {
        CrawlerToolContextData.plugins = plugins;
    }

    public static CrawlerToolContextData getInstance() {
        return instance;
    }

    private CrawlerToolContextData() {
    }

    /**
     * @return the crawlerToolRunCycle
     */
    public ICrawlerToolRunCycle getCrawlerToolRunCycle() {
        return crawlerToolRunCycle;
    }

    /**
     * @param crawlerToolRunCycle the crawlerToolRunCycle to set
     */
    public void setCrawlerToolRunCycle(ICrawlerToolRunCycle crawlerToolRunCycle) {
        this.crawlerToolRunCycle = crawlerToolRunCycle;
    }

    /**
     * @return the crawlerToolTheadLock
     */
    public Object getCrawlerToolTheadLock() {
        return crawlerToolTheadLock;
    }

    /**
     * @param crawlerToolTheadLock the crawlerToolTheadLock to set
     */
    public void setCrawlerToolTheadLock(Object crawlerToolTheadLock) {
        this.crawlerToolTheadLock = crawlerToolTheadLock;
    }

    /**
     * @return the swtThread
     */
    public Thread getSwtThread() {
        return swtThread;
    }

    /**
     * @param swtThread the swtThread to set
     */
    public void setSwtThread(Thread swtThread) {
        this.swtThread = swtThread;
    }

    /**
     * @return the applicationContext
     */
    public ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * @param applicationContext the applicationContext to set
     */
    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    /**
     * @return the contextData
     */
    public Object getContextDataByKey(String key) {
        if (contextData.containsKey(key)) {
            return contextData.get(key);
        }
        return null;
    }

    /**
     * @param obj the contextData to set
     */
    public void setContextData(String key, Object obj) {
        if (StringUtils.isNotBlank(key) && null != obj) {
            contextData.put(key, obj);
        }
    }

}
